package dao.impl;

import dao.BaseDao;
import dao.MetaverseProductDao;
import entity.MetaverseProduct;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MetaverseProductDaoImpl extends BaseDao implements MetaverseProductDao {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    @Override
    public List<MetaverseProduct> metaverseList() {
        conn = super.getConnection();
        List<MetaverseProduct> list = new ArrayList<>();
        MetaverseProduct metaverseProduct = null;
        try {
            String sql = "select * from metaverse_product order by product_price desc";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                metaverseProduct = new MetaverseProduct();
                metaverseProduct.setId(rs.getInt("id"));
                metaverseProduct.setProduct_name(rs.getString("product_name"));
                metaverseProduct.setProduct_price(rs.getDouble("product_price"));
                metaverseProduct.setProduct_desc(rs.getString("product_desc"));
                metaverseProduct.setFlag(rs.getInt("flag"));
                list.add(metaverseProduct);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int metaverseAdd(MetaverseProduct metaverseProduct) {
        conn = super.getConnection();
        int count = 0;
        try{
            String sql = "insert into metaverse_product(product_name,product_price,product_desc,flag)values(?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1,metaverseProduct.getProduct_name());
            ps.setDouble(2,metaverseProduct.getProduct_price());
            ps.setString(3,metaverseProduct.getProduct_desc());
            ps.setInt(4,metaverseProduct.getFlag());
            if(ps.executeUpdate()>0){
                count = 1;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            super.getClose(conn,ps,rs);
        }
        return count;
    }

    @Override
    public int metaverseUpdate(int id, int flag) {
        conn = super.getConnection();
        int count = 0;
        try{
            String sql = "update metaverse_product set flag=? where id=?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,flag);
            ps.setInt(2,id);
            if(ps.executeUpdate()>0){
                count = 1;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            super.getClose(conn,ps,rs);
        }
        return count;
    }

    @Override
    public int metaverseDelete(int id) {
        conn = super.getConnection();
        int count = 0;
        try{
            String sql = "delete from metaverse_product where id=?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            if(ps.executeUpdate()>0){
                count = 1;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            super.getClose(conn,ps,rs);
        }
        return count;
    }
}
